<!--
 * @Descripttion: 出手鸭小程序
 * @version:1.0.1 
 * @Author: HHL
 * @Date: 2022-11-24 09:58:36
 * @LastEditTime: 2023-11-26 22:41:48
 * @LastEditors: sueRimn
-->
<script>

import dayjs from 'dayjs'

import { clearExpData } from './utils'

require('dayjs/locale/zh-cn')
dayjs.locale('zh-cn')

export default {
	globalData: {
		token: '',
		shareMerchantId: '',
	},
	onLaunch: function(options) {
		uni.hideTabBar()
		// #ifdef MP-ALIPAY
		my.hideTabBar()
		// #endif

		this.autoUpdate()

		const token = uni.getStorageSync('token')
		if (token) {
			this.globalData.token = token
			
			// 获取用户信息
			setTimeout(() => {
				this.$store.dispatch('user/loadUserInfo')
			}, 200)
		}

		// 删除过期缓存数据
		clearExpData()


		console.log('=========onLaunch入口参数=========', options)
		const { query = {}, referrerInfo = {} } = options


		const extraData = referrerInfo.extraData || {}

		// 获取设备信息
		this.$store.dispatch('app/getSystemInfo')

		// #ifdef H5
		const platform = options.platform || undefined
		uni.setStorageSync('platform', platform)
		if (options.h5from && options.h5from == 'smallApp') {
			console.log('小程序调用h5')
			uni.setStorageSync('isSmallApp', 1)
		}
		// #endif
	},

	onShow(options) {
		console.log('==================>show', options)

		const { query = {}, referrerInfo = {} } = options

		const extraData = referrerInfo.extraData || {}

		// 芝麻速办签约后返回
		const doneOrderNo = query.doneOrderNo || undefined
		if (doneOrderNo) {
			console.log('芝麻速办签约后返回', doneOrderNo)
			setTimeout(() => {
				uni.reLaunch({
          url: `/pages/order/done/index?orderNo=${doneOrderNo}`
        })
			}, 300)
		}

		const channel = options.channel || query.channel || extraData.channel || undefined
		if (channel) {
			this.$store.dispatch('user/updateChannel', channel)
		}
	},

	methods: {
		autoUpdate() {
			if (uni.canIUse('getUpdateManager')) {
				const updateManager = uni.getUpdateManager()
				updateManager.onUpdateReady(res => {
					uni.showModal({
						title: '更新提示',
						content: '新版本已经准备好，重启应用？',
						showCancel: false,
						success: r => {
							if (r.confirm) {
								// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
								updateManager.applyUpdate();
							}
						}
					});
				});

				updateManager.onUpdateFailed(res => {
					// 新的版本下载失败
					uni.showModal({
						title: '已经有新版本了哟~',
						content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~',
						showCancel: false,
					})
				});
			}
		}
	}
}
</script>

<style>
	/*每个页面公共css */
</style>
